*------------------------------------------------------------------------------*
  *** TABLE 1. Contemporaneous effect of air pollution on voting behaviour ***
*------------------------------------------------------------------------------*

***PROGRAM FOR TURNOUT
cap program drop reg_tab2_turnout
program define reg_tab2_turnout, sclass
	args cmd spec 
	/* ARGUMENTS
	cmd: command (reg or areg)
	spec: number of the specification 
	*/

	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $depvars_tab2_turnout // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $depvars_tab2_turnout
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10_`spec'_`i'=b[1,1] // PM10 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10_`spec'_`i'  =sqrt(V[1,1]) // PM10 SE
		scalar r2`spec'_`i'  = e(r2) // r-squared
		scalar N`spec'_`i'  = e(N) // N
		scalar YM`spec'_`i' = e(ymean)  // Mean Dep. Var.
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		local pPM10_`spec'_`i'=2*(1-normal(abs(bPM10_`spec'_`i'/sePM10_`spec'_`i')))
		local starPM10_`spec'_`i' ""
		if `pPM10_`spec'_`i''<.1 {	
			local starPM10_`spec'_`i' *
		}
		if `pPM10_`spec'_`i''<.05 {
			local starPM10_`spec'_`i' **
		}
		if `pPM10_`spec'_`i''<.01 {
			local starPM10_`spec'_`i' ***
		}
		global starPM10_`spec'_`i' = "`starPM10_`spec'_`i''"
	}  // i
end

*------------------------------------------------------------------------------*

***PROGRAM FOR VOTING OUTCOMES
cap program drop reg_tab2_votes
program define reg_tab2_votes, sclass
	args cmd spec 
	
	// THE FOLLOWING LOOP RUNS THE ANALYSIS SEPARATELY FOR ALL DEPENDENT VARIABLES
	local Y : word count $depvars_tab2_votes // PICKS EACH VARIABLE ONE BY ONE FROM MACRO
	forvalues i=1/`Y' {
		local y : word `i' of $depvars_tab2_votes
		`cmd' `y' ${spec`spec'} // THIS RUNS THE REGRESSION
		estadd ysumm
		
		mat b = e(b) // THIS SAVES THE COEFFICIENT VECTOR
		scalar bPM10_`spec'_`i'=b[1,1] // PM10 coefficient
		
		mat V = e(V) // THIS SAVES THE VARIANCE MATRIX
		
		// THE FOLLOWING SAVES STANDARD ERRORS, R2 AND N AS SCALARS
		scalar sePM10_`spec'_`i'  =sqrt(V[1,1]) // PM10 SE
		scalar r2`spec'_`i'  = e(r2) // r-squared
		scalar N`spec'_`i'  = e(N) // N
		scalar YM`spec'_`i' = e(ymean)  // Mean Dep. Var.
		
		// THE FOLLOWING PRODUCES SIGNIFICANCE STARS AND SAVES THEM AS LOCALS
		local pPM10_`spec'_`i'=2*(1-normal(abs(bPM10_`spec'_`i'/sePM10_`spec'_`i')))
		local starPM10_`spec'_`i' ""
		if `pPM10_`spec'_`i''<.1 {	
			local starPM10_`spec'_`i' *
		}
		if `pPM10_`spec'_`i''<.05 {
			local starPM10_`spec'_`i' **
		}
		if `pPM10_`spec'_`i''<.01 {
			local starPM10_`spec'_`i' ***
		}
		global starPM10_`spec'_`i' = "`starPM10_`spec'_`i''"
	}  // i
end

*------------------------------------------------------------------------------*

***Macros
global depvars_tab2_turnout "turnout"
global depvars_tab2_votes "Incumbent_share Est_opp_share OtherParties_share"
global weather_controls "O3_30km_10ug temp_30km humid_rel_30km wind_speed_30km precip_30km"
global demo_econ_controls "pop_tot GDP_capita employment"

// THE FOLLOWING ARE ALL THE SPECIFICATIONS WE RUN (X-VARIABLES PLUS OPTIONS)

// Specifications in 10s of micrograms
	// Turnout. Unconditional relationship PM10
	global spec5 "PM10_30km_10ug, ${hdfe_opt}, if samplePM10_allcontrols == 1"
	// Turnout. PM10 with Ozone and weather controls, demographic and economic controls, and municipality, date and type fixed effects
	global spec6 "PM10_30km_10ug $weather_controls $demo_econ_controls i.type, ${hdfe_opt}, if samplePM10_allcontrols == 1"
	// Voting outcomes. Unconditional relationship PM10
	global spec7 "PM10_30km_10ug, ${hdfe_opt}, if samplePM10_allcontrols == 1"
	// Voting outcomes. PM10 with Ozone and weather controls, demographic and economic controls, and municipality, date and type fixed effects
	global spec8 "PM10_30km_10ug $weather_controls $demo_econ_controls turnout i.type, ${hdfe_opt}, if samplePM10_allcontrols == 1"


*------------------------------------------------------------------------------*

use "${tempdata}kreise_complete_all.dta", clear

*------------------------------------------------------------------------------*

***Regressions
*Tunrount 10s micrograms
forvalues j=5/6 {
	qui reg_tab2_turnout reghdfe `j'
}
*Voting outcomes 10s micrograms
forvalues j=7/8 {
	qui reg_tab2_votes reghdfe `j'
}

*------------------------------------------------------------------------------*

***Generate Table 1
gen str table=""

// PM10 coefficients
local i=1
local line "PM10 ($10\mu g / m^3$)&"
local line "`line'" $f_b bPM10_7_1 "\sym{$starPM10_7_1}" "&" $f_b bPM10_8_1 "\sym{$starPM10_8_1}" "&" $f_b bPM10_5_1 "\sym{$starPM10_5_1}" "&" $f_b bPM10_6_1 "\sym{$starPM10_6_1}" " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// PM10 s.e.
local i=`i'+1
local line "&" "(" $f_se sePM10_7_1 ")" "&" "(" $f_se sePM10_8_1 ")" "&" "(" $f_se sePM10_5_1 ")" "&" "(" $f_se sePM10_6_1 ")"  " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Y mean
local i=`i'+1
local line "Mean dep. var.&"
local line "`line'" $f_YM YM7_1 "&" $f_YM YM8_1 "&" $f_YM YM5_1 "&" $f_YM YM6_1 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// R2
local i=`i'+1
local line "R$^2$ & "
local line "`line'" $f_r2 r27_1 "&" $f_r2 r28_1 "&" $f_r2 r25_1 "&" $f_r2 r26_1 " \\ " 
local line: display "`line'" 
qui replace table="`line'" in `i'
// Observations
local i=`i'+1
local line "N&"
local line "`line'" $f_N N7_1 "&" $f_N N8_1 "&" $f_N N5_1 "&" $f_N N6_1 " \\ "
local line: display "`line'" 
qui replace table="`line'" in `i'
// CONTROLS
local i=`i'+1
qui replace table="\multicolumn{5}{l}{\textit{Controls} } \\ " in `i'
local i=`i'+1
qui replace table="\ \ County FE & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ El. Date FE & \checkmark & \checkmark & \checkmark & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Weather & & \checkmark & & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Ozone & & \checkmark & & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Demographics & & \checkmark & & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ El. Type FE & & \checkmark & & \checkmark \\" in `i'
local i=`i'+1
qui replace table="\ \ Turnout & & \checkmark & &  %\\" in `i'
local i=`i'+1

**Create .tex file
outfile table using "${outputs}table1.tex" in 1/`i', replace noquote
drop table
macro drop star*

*------------------------------------------------------------------------------*

clear

exit
